/*
 * EnemySpriteBaseRotor.cs
 *
 * Created on November 20, 2007, 7:49 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Shapes;

using HexTD.util;
using HexTD.ExtendNet;

namespace HexTD.sprite
{
	public abstract class EnemySpriteBaseRotor : EnemySpriteBase
	{

		private Color bodyColor;
		private Brush bodyStroke;
		private PolyLineSegment bodyShape;
		private string bodySVGName;
		private bool bodyUsesSVG;
		private float bodyScale;
		private int prevPaintTime;
		protected float rotPerTime = (float)ExtendMath.DegreeToRadian(10.0);
		private RotateTransform atRotate, atCorrect;

		/** Creates a new instance of spriteBase */
		public EnemySpriteBaseRotor(string name)
			: base(name)
		{
			this.bodyUsesSVG = false;
		}

		public EnemySpriteBaseRotor(string name, bool withImage)
			: base(name)
		{
			this.bodyUsesSVG = withImage;
		}

		protected override void doInit(Context context, int path, float delay, long health, int price, Size s)
		{
			this.atRotate = new RotateTransform();
			this.atCorrect = new RotateTransform();
			base.doInit(context, path, delay, health, price, s);
		}

		public abstract Shape generateShape();

		public abstract Color generateColor();

		public abstract Brush generateStroke();

		public abstract string getSvgName();

		public override void resetScale()
		{
#if PORT
			base.resetScale();
			if (this.bodyUsesSVG)
			{
				this.bodySVGName = this.getSvgName();
				this.bodyScale = this.size.scaleMod * this.context.paintScale / 2;
				atCorrect.setToIdentity();
				atCorrect.setToTranslation(-this.bodyScale / 2, -this.bodyScale / 2);
			}
			else
			{
				this.bodyColor = this.generateColor();
				this.bodyShape = this.generateShape();
				this.bodyStroke = this.generateStroke();
			}
#endif
		}

		public override void paint(Canvas g2, int gameTime)
		{
#if PORT
			if (this.inactive || this.dead)
			{
				this.prevPaintTime = gameTime;
			}
			else
			{
				Brush defaultStroke = g2.getStroke();

				atRotate.rotate(rotPerTime * (gameTime - this.prevPaintTime));
				this.prevPaintTime = gameTime;

				RotateTransform saveXform = g2.getTransform();
				g2.transform(atTranslate);

				if (this.shielded)
				{
					this.paintShield_bottom(g2);
				}

				this.paintHealthLine(g2);

				g2.transform(atRotate);
				if (this.bodyUsesSVG)
				{
					g2.transform(this.atCorrect);
					context.renderSvgImage(this.bodySVGName, g2, (int)this.bodyScale, (int)this.bodyScale);
				}
				else
				{
					g2.setColor(this.bodyColor);
					g2.setStroke(this.bodyStroke);
					g2.draw(this.bodyShape);
				}
				g2.setTransform(saveXform);

				if (this.shielded)
				{
					g2.transform(atTranslate);
					this.paintShield_top(g2);
					g2.setTransform(saveXform);
				}

				base.paintSelected(g2);

				g2.setStroke(defaultStroke);
			}
#endif
		}
	}
}